<?php

function TimerCheck(){
$sql="select * from xAP_Timer where active=1 and NextRun<'".date("c")."'";
	$row=dbquery($sql);

	foreach($row as $value){

			TimerProcess($value);

	}
}
function TimerProcess($row){
//timer has been triggered, now run event
	xAPLog("TIMER","Run ".$row["Alias"],"");	
	
	
	//Run events
	EventRunByLink("timer".$row["TimerID"]);
	
	// now rebuild timer and set new time for next run, or delete if marked
	if ($row["DeleteAfter"]==1){
		dbexec("delete from xAP_Timer where TimerID=".$row["TimerID"]);
		xAPLog("TIMER","Delete ".$row["Alias"],"");
	}
	else
	{
		TimerRegenorate($row);
	}
}
function TimerRegenerateAll(){
	// usually this is called at startup.  It goes through all active timers and sets up the NextRun field.
	// the main lop timer uses this dattime stamp to trigger the 
	$sql="select * from xAP_Timer where active=1";
	$row=dbquery($sql);

	foreach($row as $value){

			TimerRegenorate($value);

	};
}
function TimerRegenorate($row){

	// this takes a timer row in an array and updates that timer's NextRun field with the dattime that the time will next run
	if($row["Source"]=="Sunset"){
	
	}
	elseif($row["Source"]=="Sunset"){
	
	}
	else
	{
		$basetime=date("U",strtotime($row["AtTime"]));
	}
	
	$basetime=$basetime+(intval($row["AdjustTime"])*60);

	$newtime=$basetime;
	// before now?  if so go to tmorrow?
	if ($newtime<time()){
		$newtime=$newtime + 24*60*60;
	}
	//security randomise +- 10 minutes
	if ($row["Security"]==1){
		$newtime=$newtime+(rand(0,1200)-600);
	}
	// now  loop checking days if found invalid then move forward 1 day and try again
	$x=0;
	while($x<5000){
		// check month
		$skip=0;
		$m=date("n",$newtime);
		if($m==1 && $row["January"]==0){$skip=1;}
		if($m==2 && $row["Febuary"]==0){$skip=1;}
		if($m==3 && $row["March"]==0){$skip=1;}
		if($m==4 && $row["April"]==0){$skip=1;}
		if($m==5 && $row["May"]==0){$skip=1;}
		if($m==6 && $row["June"]==0){$skip=1;}
		if($m==7 && $row["July"]==0){$skip=1;}
		if($m==8 && $row["August"]==0){$skip=1;}
		if($m==9 && $row["September"]==0){$skip=1;}
		if($m==10 && $row["October"]==0){$skip=1;}
		if($m==11 && $row["November"]==0){$skip=1;}
		if($m==12 && $row["December"]==0){$skip=1;}
		
		if ($row["OnlyDay"]>0){
			if($row["OnlyDay"]!=date("j",$newtime)){
				$skip=1;
			}
		}
		$d=date("N",$newtime);
		if($d==1 && $row["Monday"]==0){$skip=1;}
		if($d==2 && $row["Tuesday"]==0){$skip=1;}
		if($d==3 && $row["Wednesday"]==0){$skip=1;}
		if($d==4 && $row["Thursday"]==0){$skip=1;}
		if($d==5 && $row["Friday"]==0){$skip=1;}
		if($d==6 && $row["Saturday"]==0){$skip=1;}
		if($d==7 && $row["Sunday"]==0){$skip=1;}
		
		if ($skip==1){
			$newtime=$newtime+24*60*60;
		}
		else
		{
			break;
		}
		$x++;
	}


	xAPLog("xAP","TIMER Update ".$row["Alias"],date("r",$newtime));
	
	$sql="update xAP_timer set NextRun='".date("c",$newtime)."' where TimerID=".$row["TimerID"];
	dbexec($sql);
}
?>